//
//  SPConversationListViewController.m
//  Sport
//
//  Created by tikpen on 2018/4/29.
//  Copyright © 2018年 Cass. All rights reserved.
//

#import "SPConversationListViewController.h"
#import "SPConversationViewController.h"
#import "SPUsersListViewController.h"
#import "SPAddFriendsViewController.h"
#import "SPFriendListController.h"
#import <QMUIKit.h>
@interface SPConversationListViewController ()<EaseConversationListViewControllerDelegate,EaseConversationListViewControllerDataSource>

@end

@implementation SPConversationListViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self tableViewDidTriggerHeaderRefresh];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setDataSource:self];
    [self setDelegate:self];
    self.showRefreshHeader = YES;
    [self tableViewDidTriggerHeaderRefresh];
    [self setupData];
}

- (void)setupData{
    self.navigationItem.title = @"消息";
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithImage:nil selectedImage:nil title:@"好友" target:self action:@selector(friends:)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"tab_icon_jia" selectedImage:@"tab_icon_jia" title:nil target:self action:@selector(addFriends:)];
}
- (void)friends:(UIBarButtonItem *)barButtonItem{
//    SPUsersListViewController *usersList = [[UIStoryboard storyboardWithName:@"Message" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPUsersListViewController class])];
//    usersList.title = @"好友";
    SPFriendListController *friendsList =[[UIStoryboard storyboardWithName:@"Message" bundle:[NSBundle mainBundle]]instantiateViewControllerWithIdentifier:@"SPFriendListController"];
    [self.navigationController pushViewController:friendsList animated:YES];
}
- (void)addFriends:(UIBarButtonItem *)barButtonItem{
    SPAddFriendsViewController *addVC = [[UIStoryboard storyboardWithName:@"Message" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:NSStringFromClass([SPAddFriendsViewController class])];
    addVC.title = @"添加好友";
    [self.navigationController pushViewController:addVC animated:YES];
}




#pragma mark --环信代理方法
- (void)conversationListViewController:(EaseConversationListViewController *)conversationListViewController didSelectConversationModel:(id<IConversationModel>)conversationModel{
    EMConversation *conversation = conversationModel.conversation;
    SPConversationViewController *conversationVC = [[SPConversationViewController alloc]initWithConversationChatter:conversation.conversationId conversationType:EMConversationTypeChat];
    conversationVC.title = conversationModel.title;
    conversationVC.nickname = conversationModel.title;
    conversationVC.iconUrl = conversationModel.avatarURLPath;
    [self.navigationController pushViewController:conversationVC animated:YES];
}




- (id<IConversationModel>)conversationListViewController:(EaseConversationListViewController *)conversationListViewController
                                    modelForConversation:(EMConversation *)conversation{
    EaseConversationModel *model = [[EaseConversationModel alloc] initWithConversation:conversation];

    __block NSDictionary *contract;
    __block NSError *Error;
    NSURLSession *session = [NSURLSession sharedSession];
    NSURL *url = [NSURL URLWithString:@"http://39.108.132.194/Sport/returnUserPicAndNick.php"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    request.HTTPMethod = @"POST";
    NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
    [params setObject:conversation.conversationId forKey:@"phoneNum"];
    request.HTTPBody = [[NSString stringWithFormat:@"phoneNum=%@",conversation.conversationId] dataUsingEncoding:NSUTF8StringEncoding];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (!error) {
            NSArray *dict = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableLeaves) error:nil];
            NSDictionary *arr = dict.firstObject;
            contract = arr;
        }else{
            NSLog(@"失败");
            Error = error;
        }
    }];
    [dataTask resume];
    while (!contract&&!Error) {
    
    }
    model.avatarURLPath =[contract objectForKey:@"headimage"];
    model.title = [contract objectForKey:@"nickname"];
    return model;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
